package com.lhkj.ct.admin.system.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.admin.model.entity.TblDepartment;
import com.lhkj.ct.meta.admin.model.vo.DepartmentVo;
import com.lhkj.ct.meta.admin.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *       部门管理
 * </p>
 *
 * @author jobob
 * @since 2023-08-31
 */
@RestController
@RequestMapping("api/system/department")
@ApiSupport(order = 6)
@Api(tags = {"06.部门接口"})
//@RequiresRoles(value = {SystemConstants.ADMIN_ROLE_CODE})
public class SysDeptController {

    @Resource
    private SysDeptService sysDeptService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "部门树级列表", notes = "列表查询")
    @GetMapping("tree")
    //@RequiresPermissions(value = {"system:department:list", "system:role:menus"}, logical = Logical.OR)
    public ActionResult<List<DepartmentVo>> listDepartments(TblDepartment department) {
        return ActionResult.success(sysDeptService.listDepartments(department));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取部门下拉选项", notes = "organId为机构id")
    @GetMapping("{organId}/options")
    public ActionResult<List<Option<String>>> listDeptOptions(@PathVariable String organId) {
        return ActionResult.success(sysDeptService.listOptions(organId));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "查看", notes = "查看部门详情")
    @GetMapping("get/{id}")
    //@RequiresPermissions(value = {"system:department:edit", "system:department:query"}, logical = Logical.OR)
    public ActionResult<?> get(@PathVariable @NotBlank(message = "id不能为空") String id) {
        TblDepartment target = sysDeptService.getById(id);
        if (target == null) {
            return ActionResult.fail("菜单不存在或已删除！");
        }
        return ActionResult.success(target);
    }

    @ApiOperationSupport(order = 4)
    @PostMapping("insert")
    @ApiOperation(value = "新增", notes = "新增部门")
    @Log(title = "科室管理", actionType = ActionType.INSERT)
    //@RequiresPermissions(value = "system:department:insert")
    public ActionResult<String> insert(@Validated(ValGroups.Insert.class) @RequestBody TblDepartment entity) {
        return ActionResult.success("成功",sysDeptService.saveDept(entity));
    }

    @ApiOperationSupport(order = 5)
    @PutMapping("update")
    @ApiOperation(value = "编辑", notes = "编辑部门")
    @Log(title = "科室管理", actionType = ActionType.UPDATE)
    //@RequiresPermissions(value = "system:department:edit")
    public ActionResult<String> update(@Validated(ValGroups.Insert.class) @RequestBody TblDepartment entity) {
        return ActionResult.success("成功",sysDeptService.updateDept(entity));
    }

    @ApiOperationSupport(order = 6)
    @DeleteMapping("delete")
    @ApiOperation(value = "批量删除", notes = "删除部门")
    @Log(title = "科室管理", actionType = ActionType.DELETE)
    //@RequiresPermissions(value = "system:department:delete")
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "id不能为空") List<String> ids) {
        if (sysDeptService.removeByIds(ids) > 0) {
            return ActionResult.success("删除成功");
        }
        return ActionResult.fail("删除失败");
    }

}
